package cn.com.egova.materialdesignexample.recyclerview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by yuanchao on 2017/4/9.
 * worked with LnearLayoutManger only.
 */

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    //使用系统自带的listDivider.
    //为什么要用系统自带的listDivider?方便我们去随意的改变，该属性我们可以直接声明在：
//     <!-- Application theme. -->
//    <style name="AppTheme" parent="AppBaseTheme">
//      <item name="android:listDivider">@drawable/divider_bg</item>
//    </style>
//    然后随意去写一个divider_bg即可.就如同本例一样.
    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
    //分割线Drawable资源.
    private Drawable mDivider;
    //布局方向.
    private int mOrientation;

    public DividerItemDecoration(Context context, int orientation){
        //使用TypeArray加载该系统资源
        final TypedArray ta = context.obtainStyledAttributes(ATTRS);
        mDivider = ta.getDrawable(0);
        //缓存
        ta.recycle();
        setOrientation(orientation);

//        mDivider = context.getResources().getDrawable(R.drawable.divider_bg);   //divider也可以直接这么赋值得到，但是不能直接从color值得到.
    }

    public void setOrientation(int orientation){
        if(orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST){
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if(mOrientation == VERTICAL_LIST){
            drawVertical(c,parent);
        }else{
            drawHorizontal(c,parent);
        }
    }

    /**
     * 垂直布局时,所有分割线的left,right是固定的.
     * @param c
     * @param parent
     */
    public void drawVertical(Canvas c,RecyclerView parent){
        //分割线起始点的X坐标,即RecyclerView的paddingLeft值
        final int left = parent.getPaddingLeft();
        //分割线结束点的X坐标.
        final int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        //遍历所有item view，为它们的下方绘制分割线
        for(int i=0;i<childCount;i++){
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            //分割线起始点的Y坐标.
			final int top = child.getBottom() + params.bottomMargin;
			//分割线结束点的Y坐标.
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left,top,right,bottom);
            mDivider.draw(c);
        }
    }

    /**
     * 水平布局时,所有分割线top,bottom是固定的.
     * @param c
     * @param parent
     */
    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if(mOrientation == VERTICAL_LIST){
            //设置偏移的高度是mDivider.getIntrinsicHeight，该高度正是分割线的高度
            outRect.set(0,0,0,mDivider.getIntrinsicHeight()); //相当于设置了paddingBottom
        }else{
            outRect.set(0,0,mDivider.getIntrinsicWidth(),0);  //相当于设置了paddingRight
        }
    }
}